package com.csdj.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csdj.server.mapper.EmployeeMapper;
import com.csdj.server.mapper.MailLogMapper;
import com.csdj.server.pojo.*;
import com.csdj.server.service.IEmployeeService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZouLi
 * @since 2022-12-12
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    /**
     * 注入EmployeeMapper
     **/
    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired(required = false)
    private MailLogMapper mailLogMapper;

    @Override
    public RespPageBean getEmployeeByPage(Integer page, Integer limit, Employee employee, LocalDate[] beginDateScope) {
        // 分页
        Page<Employee> employeePage = new Page<>(page, limit);
        // 查询
        IPage<Employee> iPage = employeeMapper.getEmployeeByPage(employeePage, employee, beginDateScope);
        // 返回
        return new RespPageBean(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public RespBean getMaxWorkId() {
        List<Map<String, Object>> list = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workID)"));
        int maxworkid = Integer.parseInt(list.get(0).get("max(workID)").toString())+1;
        return RespBean.success(null,String.format("%08d",maxworkid));
    }

    @Override
    public RespBean insetEmployee(Employee employee) {
        // 合同起始日期
        LocalDate beginContract = employee.getBeginContract();
        // 合同结束日期
        LocalDate endContract = employee.getEndContract();
        // 根据合同起始日期 和 合同结束日期 计算 合同期限
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        // 指定计算年限 保留两位小数
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        // 根据期限日期计算期限年限
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days / 365.00)));

        if (1 == employeeMapper.insert(employee)){
            // 获取添加的数据
            Employee emp = employeeMapper.getEmployeeById(employee.getId()).get(0);

            // 消息id
            String msgId = UUID.randomUUID().toString();
            // 数据库邮件记录
            MailLog mailLog = new MailLog();
            mailLog.setMsgId(msgId);
            mailLog.setEid(emp.getId());
            mailLog.setStatus(0);
            mailLog.setRouteKey(MailConstants.MAIL_ROUTING_NAME);
            mailLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailLog.setCount(0);
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT));
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());
            // 添加邮件记录
            mailLogMapper.insert(mailLog);
            // 发送消息
            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME, MailConstants.MAIL_ROUTING_NAME,
                    emp, new CorrelationData(msgId));

            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    @Override
    public List<Employee> getEmployeeById(Integer id) {
        return employeeMapper.getEmployeeById(id);
    }

    @Override
    public RespPageBean getEmployeeWithSalary(Integer page, Integer limit) {
        Page<Employee> employeePage = new Page<>(page, limit);
        IPage<Employee> employee = employeeMapper.getEmployeeWithSalary(employeePage);
        return new RespPageBean(employee.getTotal(),employee.getRecords());
    }
}
